package org.gaojianqiao.electronichandbook.database;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import org.gaojianqiao.electronichandbook.createtable.CheckMaintainRecordTab;
import zyg.login.DataBas;

public class OperateCheckMaintainRecordTab
{
	public ArrayList<CheckMaintainRecordTab> CheckMaintainRecordlist(
			String bookName) throws SQLException, UnsupportedEncodingException
	{

		// TODO Auto-generated method stub

		String sql = "select * from " + bookName + " order by id";
		DataBas publicDataBase = new DataBas();
		publicDataBase.connectDB("data3");
		ResultSet rs = publicDataBase.stmt.executeQuery(sql);
		ArrayList<CheckMaintainRecordTab> CheckMaintainRecordList = new ArrayList<CheckMaintainRecordTab>();
		while (rs.next())
		{
			CheckMaintainRecordTab CheckMaintainRecord = new CheckMaintainRecordTab();
			CheckMaintainRecord.setid(rs.getInt("id"));
			CheckMaintainRecord.setColumn1(rs.getString("Column1"));
			CheckMaintainRecord.setColumn2(rs.getString("Column2"));
			CheckMaintainRecord.setColumn3(rs.getString("Column3"));
			CheckMaintainRecord.setColumn4(rs.getString("Column4"));
			// CheckMaintainRecord.settext(rs.getString("weapon_text"));
			CheckMaintainRecordList.add(CheckMaintainRecord);
			// System.out.println(""+rs.getString("weapon_id")+rs.getString("weapon_title")+rs.getString("weapon_text"));
		}
		publicDataBase.closeDB();

		return CheckMaintainRecordList;
	}

	public void insert(ArrayList<CheckMaintainRecordTab> str, String tablename)
			throws SQLException, ClassNotFoundException
	{
		String sql = "insert into " + tablename + "  values (?,?,?,?,?)";
		DataBas publicDataBase = new DataBas();
		Connection conn = publicDataBase.connectDB("public");
		PreparedStatement ps = conn.prepareStatement(sql);
		int i = 0;
		do
		{
			ps.setInt(1, str.get(i).getid());
			ps.setString(2, str.get(i).getColumn1());
			ps.setString(3, str.get(i).getColumn2());
			ps.setString(4, str.get(i).getColumn3());
			ps.setString(4, str.get(i).getColumn4());
			ps.executeUpdate();
			++i;
		} while (i < str.size());
		ps.close();
		conn.close();

	}

	public void createtable(String tablename) throws SQLException,
			ClassNotFoundException
	{

		String sql2 = "drop table if exists " + tablename;
		String sql3 = "create table "
				+ tablename
				+ " (id integer,Column1 text,Column2 text,Column3 text,Column4 text)";
		DataBas publicDataBase = new DataBas();
		Connection conn = publicDataBase.connectDB("data3");
		Statement stat = conn.createStatement();

		conn.setAutoCommit(false);
		stat.executeUpdate(sql2);
		stat.executeUpdate(sql3);
		conn.commit();
		conn.close();
	}
	//

}
